package com.ufm.security.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ufm.security.dao.RightsMapper;
import com.ufm.security.pojo.Rights;
import com.ufm.security.pojo.RightsAndRoles;
import com.ufm.security.pojo.UsersAndRoles;
import com.ufm.security.service.IRightsService;
@Service(value="rightsService")
@Transactional(rollbackFor=Exception.class)
public class RightsServiceImpl implements IRightsService {
	@Autowired
	RightsMapper rightsMapper;
	private int userId;

	@Override
	public List<Rights> findAll(Map params) {
		return rightsMapper.findAll(params);
	}

	@Override
	public List<Rights> findByRoleId(int roleId) {
		return rightsMapper.findByRoleId(roleId);
	}

	@Override
	public void saveRoleAndRights(RightsAndRoles rightsAndRoles) {
		rightsMapper.saveRoleAndRights(rightsAndRoles);
	}
	
	@Override
	public void deleteRoleAndRights(int roleId) {
		rightsMapper.deleteRoleAndRights(roleId);
	}

	@Override
	public void updateRoleAndRights(String vRightsId, int roleId) {
		deleteRoleAndRights(roleId);
		if(vRightsId.indexOf(",")!=-1){
			String[] temp = vRightsId.split(",");
			for(int i=0;i<temp.length;i++){
				RightsAndRoles rightsAndRoles =new RightsAndRoles();
				rightsAndRoles.setRight_id(Integer.parseInt(temp[i].toString()));
				rightsAndRoles.setRole_id(roleId);
				saveRoleAndRights(rightsAndRoles);
			}
		}else{
			RightsAndRoles rightsAndRoles =new RightsAndRoles();
			rightsAndRoles.setRight_id(Integer.parseInt(vRightsId));
			rightsAndRoles.setRole_id(roleId);
			saveRoleAndRights(rightsAndRoles);
		}
	}

	@Override
	public void updateRoleAndUsers(String vUsersId, int roleId) {
		deleteRoleAndUsers(roleId);
		//vUsersId=null; 
		if("".equals(vUsersId)){
			//
		}else{
			if(vUsersId.indexOf(",")!=-1){
				String[] temp = vUsersId.split(",");
				for(int i=0;i<temp.length;i++){
					if(temp[i].toString().indexOf("d")==-1){
						UsersAndRoles usersAndRoles =new UsersAndRoles();
						usersAndRoles.setUsers_id(Integer.parseInt(temp[i].toString().replace("u", "")));
						usersAndRoles.setRole_id(roleId);
						saveRoleAndUsers(usersAndRoles);
					}
				}
			}else{
				UsersAndRoles usersAndRoles =new UsersAndRoles();
				usersAndRoles.setUsers_id(Integer.parseInt(vUsersId.replace("u", "")));
				usersAndRoles.setRole_id(roleId);
				saveRoleAndUsers(usersAndRoles);
			}
		}
	}

	@Override
	public void deleteRoleAndUsers(int roleId) {
		rightsMapper.deleteRoleAndUsers(roleId);
		
	}

	@Override
	public void saveRoleAndUsers(UsersAndRoles usersAndRoles) {
		rightsMapper.saveRoleAndUsers(usersAndRoles);
	}
	
	@Override
	public List<Integer> findRoleByUserId(int userId){
		return rightsMapper.findRoleByUserId(userId);
	}
	
	@Override
	public void deleteRoleByUserId(int userId){
		rightsMapper.deleteRoleByUserId(userId);
	}
}
